<template>
  <div class="editor-tab">
    <div class="title">
      <div>
        <div class="circle" v-show="isModify"></div>
        <span>{{ editorItem.title }}</span>
      </div>
      <el-button @click="submitCurrentTab()" :link="true" type="primary"
        >保存</el-button
      >
    </div>
    <component
      :is="editorItem.path"
      :tab="tab"
      ref="childrenRef"
      v-if="flag"
    ></component>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref, onMounted } from 'vue'
import { useStore } from '@/render/store'
import BaseConfiguration from '@/render/views/project/baseconfig/baseconfig.vue'
import ModuleConfigDef from '@/render/views/project/module/configdef/configdef.vue'
import ModuleDrives from '@/render/views/project/module/drives/drives.vue'
import { ITabState } from '@/render/store/base/types'
import emitter from '@/render/utils/mitt'

export default defineComponent({
  components: {
    BaseConfiguration,
    ModuleConfigDef,
    ModuleDrives
  },
  props: {
    editorItem: {
      type: Object,
      required: true
    },
    editorIndex: {
      type: Number,
      required: true
    }
  },
  setup(props) {
    const store = useStore()
    const tab = ref<ITabState>()
    const isModify = ref(false)
    const flag = ref(false)
    const childrenRef = ref()

    const submitCurrentTab = () => {
      childrenRef.value?.update()
      isModify.value = false
    }

    onMounted(() => {
      tab.value = store.getters['base/getActiveTab'](props.editorIndex)
      flag.value = true
    })

    emitter.on('update-tab', () => {
      isModify.value = true
    })

    return { childrenRef, flag, isModify, tab, submitCurrentTab }
  }
})
</script>

<style scoped lang="less">
@import '../../../assets/style/index';
.editor-tab {
  padding: 10px;
  .title {
    display: flex;
    justify-content: space-between;
    align-items: center;
    border: @content-border-style;
    border-radius: 5px;
    padding: 0 10px;
    height: @content-row-height;
    & > div:nth-child(1) {
      display: flex;
      align-items: center;
      .circle {
        margin: 10px;
        background-color: #409eff;
        border: #409eff 2px solid;
        width: 8px;
        height: 8px;
        border-radius: 50%;
      }
      span {
        font-weight: 500;
        font-size: var(--el-font-size-base);
      }
    }
  }
}
</style>
